﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Pongster
{
    public class TitleScreen : BaseGameState
    {
        Texture2D backgroundImage;
        LinkLabel startLabel;
        Label funFact;
        Label copyRight;

        Random rnd = new Random();
        int message;

        public TitleScreen(Game game, GameStateManager manager)
            : base(game, manager)
        {
        }

        public override void Initialize()
        {            
            base.Initialize();

            GameRef.SoundManager.StopAll();
            GameRef.SoundManager.StopSong();
            GameRef.SoundManager.PlaySong("titletrack", true); 
            
            message = rnd.Next(0, 50);
 
        }

        protected override void LoadContent()
        {
            ContentManager Content = GameRef.Content;

            backgroundImage = Content.Load<Texture2D>(@"Backgrounds\titlebg");
            GameRef.SoundManager.LoadSong("titletrack", @"Audio\Music\titletrack");

            base.LoadContent();

            startLabel = new LinkLabel();            
            startLabel.Text = "Press ENTER to begin";
            startLabel.Position = new Vector2(((float)GameRef.BaseRectangle.Width - startLabel.SpriteFont.MeasureString(startLabel.Text).X) / 2f, (float)GameRef.BaseRectangle.Height / 1.4f);
            startLabel.Color = Color.White;
            startLabel.TabStop = true;
            startLabel.HasFocus = true;
            startLabel.Selected += new EventHandler(startLabel_Selected);
            ControlManager.Add(startLabel);


            funFact = new Label();

            funFact.Text = "> <";
            funFact.Size = funFact.SpriteFont.MeasureString(funFact.Text);
            funFact.Position = new Vector2(Settings.basescreenWidth / 2, Settings.basescreenHeight / 2);
            funFact.Origin = new Vector2(funFact.Size.X / 2, 0);
            funFact.Scale = 0.7f;
            ControlManager.Add(funFact);

            message = rnd.Next(0, 50);

            copyRight = new Label();

            copyRight.Text = "Copyright © Filip.V4 2013";
            copyRight.Size = copyRight.SpriteFont.MeasureString(copyRight.Text);
            copyRight.Position = new Vector2(Settings.basescreenWidth / 2, Settings.basescreenHeight - 70);
            copyRight.Origin = new Vector2(copyRight.Size.X / 2, 0);
            copyRight.Scale = 0.5f;
            //ControlManager.Add(copyRight);
                        
        }

        public override void Update(GameTime gameTime)
        {
            if (startLabel.HasFocus == true)
            {
                startLabel.selectedColor.G += (byte)MathHelper.Lerp(0, 240, 0.017f);
                startLabel.selectedColor.A -= (byte)MathHelper.Lerp(0, 240, 0.017f);
            }

            funFact.selectedColor.R += (byte)MathHelper.Lerp(0, 250, 0.015f); ;
            funFact.selectedColor.G += (byte)MathHelper.Lerp(0, 250, 0.015f); ;
            funFact.selectedColor.B += (byte)MathHelper.Lerp(0, 250, 0.015f); ;
            funFact.selectedColor.A += (byte)MathHelper.Lerp(0, 250, 0.015f); ;

            if(message >= 0 && message <= 10)
                funFact.Text = "> WIll you become the greatest Pongster ever!? <";
            else if (message >= 11 && message <= 23)
                funFact.Text = "> Pongster Is derived from the words Pong and Master. <";
            else if (message >= 24 && message <= 30)
                funFact.Text = "> Do you have what It takes to be the greatest Pongster ever!? <";
            else if (message >= 31 && message <= 40)
                funFact.Text = "> The ball awaits! <";
            else if (message >= 41 && message <= 50)
                funFact.Text = "> In the beginning, there was Pong! <";


            funFact.Size = funFact.SpriteFont.MeasureString(funFact.Text);
            funFact.Origin = new Vector2(funFact.Size.X / 2, 0);

            ControlManager.Update(gameTime);
            
            if (InputHandler.KeyReleased(Keys.Escape))
            {
                GameRef.SoundManager.StopAll();
                GameRef.SoundManager.StopSong();
                GameRef.Exit();
            }            

            base.Update(gameTime);
        }


        public override void Draw(GameTime gameTime)
        {
            GameRef.SpriteBatch.Begin();
          
            GameRef.SpriteBatch.Draw(backgroundImage, GameRef.BaseRectangle, Color.White);

            ControlManager.Draw(GameRef.SpriteBatch);

            base.Draw(gameTime);

            GameRef.SpriteBatch.End();
        }


        private void startLabel_Selected(object sender, EventArgs e)
        {
            GameRef.SoundManager.PlaySFX("select", 0.8f, 0, 0, null, null);

            //StateManager.PushState(GameRef.StartMenuScreen);
            Transition(ChangeType.Push, GameRef.StartMenuScreen);
        }

    }
}
